using OhioState.AssetManaged;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;
using Tao.OpenGl;

namespace OhioState.Graphics.OpenGL
{
    public class LightCollectionHandler : OpenGLObject
    {
        //
        // Note: To Oleg - by removing this variable (lightCollectionProxy) as a public property,
        //    you can avoid all of those tests for null. Now if it is passed
        //    in as null to the constructor, the base class will throw an exception.
        //    This is fine and what we want, since it should never happen. Handlers
        //    are only constructed on demand based on proxies, so if we ever have a
        //    null proxy, then soemthing is seriously wrong.
        //
        private LightCollectionProxy lightCollectionProxy;

        public LightCollectionHandler(LightCollectionProxy lightCollectionProxy)
            : base(lightCollectionProxy)
        {
            this.lightCollectionProxy = lightCollectionProxy;
        }

        public void Enable(IRenderPanel renderPanel)
        {
            //
            // TODO: Figure out a better design for this. We should not have OpenGL code
            // in the handlers. - Comment and following line added by Crawfis.
            //
            Gl.glEnable(Gl.GL_LIGHTING);

            int lightNumber = 0;
            foreach (ILight lightProxy in lightCollectionProxy.LightProxies)
            {
                ILight lightHandler = renderPanel.ResourceManager.GetItem((IAssetManaged)lightProxy) as ILight;
                if (lightHandler != null)
                {
                    lightHandler.Enable(renderPanel, lightNumber);
                    lightNumber++;
                }
            }
        }

        public void Disable(IRenderPanel renderPanel)
        {
            //
            // TODO: Figure out a better design for this. We should not have OpenGL code
            // in the handlers. - Comment and following line added by Crawfis.
            //
            Gl.glDisable(Gl.GL_LIGHTING);
            foreach (ILight lightProxy in lightCollectionProxy.LightProxies)
            {
                ILight lightHandler = renderPanel.ResourceManager.GetItem((IAssetManaged)lightProxy) as ILight;
                if (lightHandler != null)
                {
                    lightHandler.Disable(renderPanel);
                }
            }
        }

        public void Enable(IRenderPanel renderPanel, int lightPosition)
        {
            if (lightPosition >= 0 && lightPosition < lightCollectionProxy.LightProxies.Count)
            {
                IAssetManaged lightAsset = lightCollectionProxy.LightProxies[lightPosition] as IAssetManaged;
                ILight lightHandler = renderPanel.ResourceManager.GetItem(lightAsset) as ILight;
                if (lightHandler != null)
                {
                    lightHandler.Enable(renderPanel, lightPosition);
                }
            }
        }
    }
}
